/* Init */
Swepper 				= {}
Swepper.CoolTime		= 0

/* ConVars */
CreateClientConVar( "is_pos_x", 0, false, false )
CreateClientConVar( "is_pos_y", 0, false, false )
CreateClientConVar( "is_pos_z", 0, false, false )
CreateClientConVar( "is_ang_p", 0, false, false )
CreateClientConVar( "is_ang_y", 0, false, false )
CreateClientConVar( "is_ang_r", 0, false, false )
CreateClientConVar( "run_pos_x", 0, false, false )
CreateClientConVar( "run_pos_y", 0, false, false )
CreateClientConVar( "run_pos_z", 0, false, false )
CreateClientConVar( "run_ang_p", 0, false, false )
CreateClientConVar( "run_ang_y", 0, false, false )
CreateClientConVar( "run_ang_r", 0, false, false )
CreateClientConVar( "hol_pos_x", 0, false, false )
CreateClientConVar( "hol_pos_y", 0, false, false )
CreateClientConVar( "hol_pos_z", 0, false, false )
CreateClientConVar( "hol_ang_p", 0, false, false )
CreateClientConVar( "hol_ang_y", 0, false, false )
CreateClientConVar( "hol_ang_r", 0, false, false )
CreateClientConVar( "wal_pos_x", 0, false, false )
CreateClientConVar( "wal_pos_y", 0, false, false )
CreateClientConVar( "wal_pos_z", 0, false, false )
CreateClientConVar( "wal_ang_p", 0, false, false )
CreateClientConVar( "wal_ang_y", 0, false, false )
CreateClientConVar( "wal_ang_r", 0, false, false )

/* Information */
local sw				= ScrW()
local sh				= ScrH()
local sw5				= sw / 5
local sh5				= sh / 5
local sw6				= sw / 6
local sh6				= sh / 6
local sh2				= sh / 2
local sh3				= sh / 3
local sw7				= sw / 7
local sh7				= sh / 7

/*****************************************************************************************/
/**  FRAME																				**/
/*****************************************************************************************/

/* Create the menu */
local menu				= vgui.Create( "DFrame" )
menu:SetPos( sw5, sh2 - sh7 )
menu:SetSize( sw5, sh2 )
menu:SetTitle( "SWEP Configurator" )
menu:SetVisible( false )
menu:SetDraggable( true )
menu:ShowCloseButton( true )
menu:SetSizable( false )
menu.Close = function( self ) self:SetVisible( false ) end

/*****************************************************************************************/
/**  IRON SIGHTS																		**/
/*****************************************************************************************/

/* Iron Sights panel */
local isPanel			= vgui.Create( "DCollapsibleCategory", menu )
isPanel:SetLabel( "Iron Sights" )
isPanel:SetPos( 5, 25 )
isPanel:SetSize( sw5 - 10, 50 )
isPanel:SetExpanded( false )

/* Iron Sights list */
local isList			= vgui.Create( "DPanelList" )
isList:SetAutoSize( true )
isList:SetSpacing( 5 )
isList:EnableHorizontal( false )
isList:EnableVerticalScrollbar( true )
isPanel:SetContents( isList )

/* Iron Sights X Position */
local isSliderPosX		= vgui.Create( "DNumSlider" )
isSliderPosX:SetText( "Position X" )
isSliderPosX:SetSize( sw7, 30 )
isSliderPosX:SetMin( -100 )
isSliderPosX:SetMax( 100 )
isSliderPosX:SetDecimals( 2 )
isSliderPosX:SetConVar( "is_pos_x" )
isList:AddItem( isSliderPosX )

/* Iron Sights Y Position */
local isSliderPosY		= vgui.Create( "DNumSlider" )
isSliderPosY:SetText( "Position Y" )
isSliderPosY:SetSize( sw7, 30 )
isSliderPosY:SetMin( -100 )
isSliderPosY:SetMax( 100 )
isSliderPosY:SetDecimals( 2 )
isSliderPosY:SetConVar( "is_pos_y" )
isList:AddItem( isSliderPosY )

/* Iron Sights Z Position */
local isSliderPosZ		= vgui.Create( "DNumSlider" )
isSliderPosZ:SetText( "Position Z" )
isSliderPosZ:SetSize( sw7, 30 )
isSliderPosZ:SetMin( -100 )
isSliderPosZ:SetMax( 100 )
isSliderPosZ:SetDecimals( 2 )
isSliderPosZ:SetConVar( "is_pos_z" )
isList:AddItem( isSliderPosZ )

/* Iron Sights Pitch Angle */
local isSliderAngP		= vgui.Create( "DNumSlider" )
isSliderAngP:SetText( "Angles Pitch" )
isSliderAngP:SetSize( sw7, 30 )
isSliderAngP:SetMin( -180 )
isSliderAngP:SetMax( 180 )
isSliderAngP:SetDecimals( 2 )
isSliderAngP:SetConVar( "is_ang_p" )
isList:AddItem( isSliderAngP )

/* Iron Sights Yaw Angle */
local isSliderAngY		= vgui.Create( "DNumSlider" )
isSliderAngY:SetText( "Angles Yaw" )
isSliderAngY:SetSize( sw7, 30 )
isSliderAngY:SetMin( -180 )
isSliderAngY:SetMax( 180 )
isSliderAngY:SetDecimals( 2 )
isSliderAngY:SetConVar( "is_ang_y" )
isList:AddItem( isSliderAngY )

/* Iron Sights Roll Angle */
local isSliderAngR		= vgui.Create( "DNumSlider" )
isSliderAngR:SetText( "Angles Roll" )
isSliderAngR:SetSize( sw7, 30 )
isSliderAngR:SetMin( -180 )
isSliderAngR:SetMax( 180 )
isSliderAngR:SetDecimals( 2 )
isSliderAngR:SetConVar( "is_ang_r" )
isList:AddItem( isSliderAngR )

/*****************************************************************************************/
/**  RUNNING																			**/
/*****************************************************************************************/

/* Running panel */
local runPanel			= vgui.Create( "DCollapsibleCategory", menu )
runPanel:SetLabel( "Running" )
runPanel:SetPos( 5, 25 + 55 )
runPanel:SetSize( sw5 - 10, 50 )
runPanel:SetExpanded( false )

/* Running list */
local runList			= vgui.Create( "DPanelList" )
runList:SetAutoSize( true )
runList:SetSpacing( 5 )
runList:EnableHorizontal( false )
runList:EnableVerticalScrollbar( true )
runPanel:SetContents( runList )

/* Running X Position */
local runSliderPosX		= vgui.Create( "DNumSlider" )
runSliderPosX:SetText( "Position X" )
runSliderPosX:SetSize( sw7, 30 )
runSliderPosX:SetMin( -100 )
runSliderPosX:SetMax( 100 )
runSliderPosX:SetDecimals( 2 )
runSliderPosX:SetConVar( "run_pos_x" )
runList:AddItem( runSliderPosX )

/* Running Y Position */
local runSliderPosY		= vgui.Create( "DNumSlider" )
runSliderPosY:SetText( "Position Y" )
runSliderPosY:SetSize( sw7, 30 )
runSliderPosY:SetMin( -100 )
runSliderPosY:SetMax( 100 )
runSliderPosY:SetDecimals( 2 )
runSliderPosY:SetConVar( "run_pos_y" )
runList:AddItem( runSliderPosY )

/* Running Z Position */
local runSliderPosZ		= vgui.Create( "DNumSlider" )
runSliderPosZ:SetText( "Position Z" )
runSliderPosZ:SetSize( sw7, 30 )
runSliderPosZ:SetMin( -100 )
runSliderPosZ:SetMax( 100 )
runSliderPosZ:SetDecimals( 2 )
runSliderPosZ:SetConVar( "run_pos_z" )
runList:AddItem( runSliderPosZ )

/* Running Pitch Angle */
local runSliderAngP		= vgui.Create( "DNumSlider" )
runSliderAngP:SetText( "Angles Pitch" )
runSliderAngP:SetSize( sw7, 30 )
runSliderAngP:SetMin( -180 )
runSliderAngP:SetMax( 180 )
runSliderAngP:SetDecimals( 2 )
runSliderAngP:SetConVar( "run_ang_p" )
runList:AddItem( runSliderAngP )

/* Running Yaw Angle */
local runSliderAngY		= vgui.Create( "DNumSlider" )
runSliderAngY:SetText( "Angles Yaw" )
runSliderAngY:SetSize( sw7, 30 )
runSliderAngY:SetMin( -180 )
runSliderAngY:SetMax( 180 )
runSliderAngY:SetDecimals( 2 )
runSliderAngY:SetConVar( "run_ang_y" )
runList:AddItem( runSliderAngY )

/* Running Roll Angle */
local runSliderAngR		= vgui.Create( "DNumSlider" )
runSliderAngR:SetText( "Angles Roll" )
runSliderAngR:SetSize( sw7, 30 )
runSliderAngR:SetMin( -180 )
runSliderAngR:SetMax( 180 )
runSliderAngR:SetDecimals( 2 )
runSliderAngR:SetConVar( "run_ang_r" )
runList:AddItem( runSliderAngR )

/*****************************************************************************************/
/**  HOLSTER																			**/
/*****************************************************************************************/

/* Holster panel */
local holPanel			= vgui.Create( "DCollapsibleCategory", menu )
holPanel:SetLabel( "Holster" )
holPanel:SetPos( 5, 25 + 55 + 55 )
holPanel:SetSize( sw5 - 10, 50 )
holPanel:SetExpanded( false )

/* Holster list */
local holList			= vgui.Create( "DPanelList" )
holList:SetAutoSize( true )
holList:SetSpacing( 5 )
holList:EnableHorizontal( false )
holList:EnableVerticalScrollbar( true )
holPanel:SetContents( holList )

/* Holster X Position */
local holSliderPosX		= vgui.Create( "DNumSlider" )
holSliderPosX:SetText( "Position X" )
holSliderPosX:SetSize( sw7, 30 )
holSliderPosX:SetMin( -100 )
holSliderPosX:SetMax( 100 )
holSliderPosX:SetDecimals( 2 )
holSliderPosX:SetConVar( "hol_pos_x" )
holList:AddItem( holSliderPosX )

/* Holster Y Position */
local holSliderPosY		= vgui.Create( "DNumSlider" )
holSliderPosY:SetText( "Position Y" )
holSliderPosY:SetSize( sw7, 30 )
holSliderPosY:SetMin( -100 )
holSliderPosY:SetMax( 100 )
holSliderPosY:SetDecimals( 2 )
holSliderPosY:SetConVar( "hol_pos_y" )
holList:AddItem( holSliderPosY )

/* Holster Z Position */
local holSliderPosZ		= vgui.Create( "DNumSlider" )
holSliderPosZ:SetText( "Position Z" )
holSliderPosZ:SetSize( sw7, 30 )
holSliderPosZ:SetMin( -100 )
holSliderPosZ:SetMax( 100 )
holSliderPosZ:SetDecimals( 2 )
holSliderPosZ:SetConVar( "hol_pos_z" )
holList:AddItem( holSliderPosZ )

/* Holster Pitch Angle */
local holSliderAngP		= vgui.Create( "DNumSlider" )
holSliderAngP:SetText( "Angles Pitch" )
holSliderAngP:SetSize( sw7, 30 )
holSliderAngP:SetMin( -180 )
holSliderAngP:SetMax( 180 )
holSliderAngP:SetDecimals( 2 )
holSliderAngP:SetConVar( "hol_ang_p" )
holList:AddItem( holSliderAngP )

/* Holster Yaw Angle */
local holSliderAngY		= vgui.Create( "DNumSlider" )
holSliderAngY:SetText( "Angles Yaw" )
holSliderAngY:SetSize( sw7, 30 )
holSliderAngY:SetMin( -180 )
holSliderAngY:SetMax( 180 )
holSliderAngY:SetDecimals( 2 )
holSliderAngY:SetConVar( "hol_ang_y" )
holList:AddItem( holSliderAngY )

/* Holster Roll Angle */
local holSliderAngR		= vgui.Create( "DNumSlider" )
holSliderAngR:SetText( "Angles Roll" )
holSliderAngR:SetSize( sw7, 30 )
holSliderAngR:SetMin( -180 )
holSliderAngR:SetMax( 180 )
holSliderAngR:SetDecimals( 2 )
holSliderAngR:SetConVar( "hol_ang_r" )
holList:AddItem( holSliderAngR )

/*****************************************************************************************/
/**  CLOSE WALLS																		**/
/*****************************************************************************************/

/* Wall panel */
local walPanel			= vgui.Create( "DCollapsibleCategory", menu )
walPanel:SetLabel( "Close walls" )
walPanel:SetPos( 5, 25 + 55 + 55 )
walPanel:SetSize( sw5 - 10, 50 )
walPanel:SetExpanded( false )

/* Wall list */
local walList			= vgui.Create( "DPanelList" )
walList:SetAutoSize( true )
walList:SetSpacing( 5 )
walList:EnableHorizontal( false )
walList:EnableVerticalScrollbar( true )
walPanel:SetContents( walList )

/* Wall X Position */
local holSliderPosX		= vgui.Create( "DNumSlider" )
holSliderPosX:SetText( "Position X" )
holSliderPosX:SetSize( sw7, 30 )
holSliderPosX:SetMin( -100 )
holSliderPosX:SetMax( 100 )
holSliderPosX:SetDecimals( 2 )
holSliderPosX:SetConVar( "wal_pos_x" )
walList:AddItem( holSliderPosX )

/* Wall Y Position */
local holSliderPosY		= vgui.Create( "DNumSlider" )
holSliderPosY:SetText( "Position Y" )
holSliderPosY:SetSize( sw7, 30 )
holSliderPosY:SetMin( -100 )
holSliderPosY:SetMax( 100 )
holSliderPosY:SetDecimals( 2 )
holSliderPosY:SetConVar( "wal_pos_y" )
walList:AddItem( holSliderPosY )

/* Wall Z Position */
local holSliderPosZ		= vgui.Create( "DNumSlider" )
holSliderPosZ:SetText( "Position Z" )
holSliderPosZ:SetSize( sw7, 30 )
holSliderPosZ:SetMin( -100 )
holSliderPosZ:SetMax( 100 )
holSliderPosZ:SetDecimals( 2 )
holSliderPosZ:SetConVar( "wal_pos_z" )
walList:AddItem( holSliderPosZ )

/* Wall Pitch Angle */
local holSliderAngP		= vgui.Create( "DNumSlider" )
holSliderAngP:SetText( "Angles Pitch" )
holSliderAngP:SetSize( sw7, 30 )
holSliderAngP:SetMin( -180 )
holSliderAngP:SetMax( 180 )
holSliderAngP:SetDecimals( 2 )
holSliderAngP:SetConVar( "wal_ang_p" )
walList:AddItem( holSliderAngP )

/* Wall Yaw Angle */
local holSliderAngY		= vgui.Create( "DNumSlider" )
holSliderAngY:SetText( "Angles Yaw" )
holSliderAngY:SetSize( sw7, 30 )
holSliderAngY:SetMin( -180 )
holSliderAngY:SetMax( 180 )
holSliderAngY:SetDecimals( 2 )
holSliderAngY:SetConVar( "wal_ang_y" )
walList:AddItem( holSliderAngY )

/* Wall Roll Angle */
local holSliderAngR		= vgui.Create( "DNumSlider" )
holSliderAngR:SetText( "Angles Roll" )
holSliderAngR:SetSize( sw7, 30 )
holSliderAngR:SetMin( -180 )
holSliderAngR:SetMax( 180 )
holSliderAngR:SetDecimals( 2 )
holSliderAngR:SetConVar( "wal_ang_r" )
walList:AddItem( holSliderAngR )

/* Register menu */
Swepper.Menu			= menu

/*****************************************************************************************/

/* Open the menu */
Swepper.Open = function( self, swep )
	if self.CoolTime < CurTime() then
		
		/* Use SWEP config for Iron Sights */
		RunConsoleCommand( "is_pos_x", tostring( swep.IronSights.Position.x ) )
		RunConsoleCommand( "is_pos_y", tostring( swep.IronSights.Position.y ) )
		RunConsoleCommand( "is_pos_z", tostring( swep.IronSights.Position.z ) )
		RunConsoleCommand( "is_ang_p", tostring( swep.IronSights.Angles.x ) )
		RunConsoleCommand( "is_ang_y", tostring( swep.IronSights.Angles.y ) )
		RunConsoleCommand( "is_ang_r", tostring( swep.IronSights.Angles.z ) )
		
		/* Use SWEP config for Running */
		RunConsoleCommand( "run_pos_x", tostring( swep.RunInfo.Position.x ) )
		RunConsoleCommand( "run_pos_y", tostring( swep.RunInfo.Position.y ) )
		RunConsoleCommand( "run_pos_z", tostring( swep.RunInfo.Position.z ) )
		RunConsoleCommand( "run_ang_p", tostring( swep.RunInfo.Angles.x ) )
		RunConsoleCommand( "run_ang_y", tostring( swep.RunInfo.Angles.y ) )
		RunConsoleCommand( "run_ang_r", tostring( swep.RunInfo.Angles.z ) )
		
		/* Use SWEP config for Holster */
		RunConsoleCommand( "hol_pos_x", tostring( swep.HolsterInfo.Position.x ) )
		RunConsoleCommand( "hol_pos_y", tostring( swep.HolsterInfo.Position.y ) )
		RunConsoleCommand( "hol_pos_z", tostring( swep.HolsterInfo.Position.z ) )
		RunConsoleCommand( "hol_ang_p", tostring( swep.HolsterInfo.Angles.x ) )
		RunConsoleCommand( "hol_ang_y", tostring( swep.HolsterInfo.Angles.y ) )
		RunConsoleCommand( "hol_ang_r", tostring( swep.HolsterInfo.Angles.z ) )
		
		/* Use SWEP config for Walls */
		if not swep.HolsterInfo.Active then
			RunConsoleCommand( "wal_pos_x", tostring( swep.WallInfo.MaxPosition.x ) )
			RunConsoleCommand( "wal_pos_y", tostring( swep.WallInfo.MaxPosition.y ) )
			RunConsoleCommand( "wal_pos_z", tostring( swep.WallInfo.MaxPosition.z ) )
			RunConsoleCommand( "wal_ang_p", tostring( swep.WallInfo.MaxAngles.x ) )
			RunConsoleCommand( "wal_ang_y", tostring( swep.WallInfo.MaxAngles.y ) )
			RunConsoleCommand( "wal_ang_r", tostring( swep.WallInfo.MaxAngles.z ) )
		else
			RunConsoleCommand( "wal_pos_x", tostring( swep.WallInfo.MinPosition.x ) )
			RunConsoleCommand( "wal_pos_y", tostring( swep.WallInfo.MinPosition.y ) )
			RunConsoleCommand( "wal_pos_z", tostring( swep.WallInfo.MinPosition.z ) )
			RunConsoleCommand( "wal_ang_p", tostring( swep.WallInfo.MinAngles.x ) )
			RunConsoleCommand( "wal_ang_y", tostring( swep.WallInfo.MinAngles.y ) )
			RunConsoleCommand( "wal_ang_r", tostring( swep.WallInfo.MinAngles.z ) )
		end
		
		/* Show menu */
		self.CoolTime		= CurTime() + 2
		menu:SetVisible( true )
		menu:MakePopup()
	end
end

/* Close the menu */
Swepper.Close = function( self )
	menu:Close()
end

/* Check visibility */
Swepper.IsOpen = function( self )
	return menu:IsVisible()
end

/* Check iron sights */
Swepper.IsIronSightsActive = function( self )
	return isPanel:GetExpanded()
end

/* Check running */
Swepper.IsRunningActive = function( self )
	return runPanel:GetExpanded()
end

/* Check holster */
Swepper.IsHolsterActive = function( self )
	return holPanel:GetExpanded()
end

/* Check wall */
Swepper.IsWallActive = function( self )
	return walPanel:GetExpanded()
end

/* Collapse panels if one is clicked */
isPanel.OnCursorEntered = function( self )
	runPanel:SetExpanded( false )
	holPanel:SetExpanded( false )
	walPanel:SetExpanded( false )
end

/* Collapse panels if one is clicked */
runPanel.OnCursorEntered = function( self )
	isPanel:SetExpanded( false )
	holPanel:SetExpanded( false )
	walPanel:SetExpanded( false )
end

/* Collapse panels if one is clicked */
holPanel.OnCursorEntered = function( self )
	isPanel:SetExpanded( false )
	runPanel:SetExpanded( false )
	walPanel:SetExpanded( false )
end

/* Collapse panels if one is clicked */
walPanel.OnCursorEntered = function( self )
	isPanel:SetExpanded( false )
	runPanel:SetExpanded( false )
	holPanel:SetExpanded( false )
end

runPanel.Think = function( self )
	local isX,isY		= isPanel:GetPos()
	local isW,isH		= isPanel:GetSize()
	runPanel:SetPos( 5, isY + isH + 5 )
end

holPanel.Think = function( self )
	local runX,runY		= runPanel:GetPos()
	local runW,runH		= runPanel:GetSize()
	holPanel:SetPos( 5, runY + runH + 5 )
end

walPanel.Think = function( self )
	local holX,holY		= holPanel:GetPos()
	local holW,holH		= holPanel:GetSize()
	walPanel:SetPos( 5, holY + holH + 5 )
end

menu.Think = function( self )

	/* Get panels positions */
	local isX,isY		= isPanel:GetPos()
	local runX,runY		= runPanel:GetPos()
	local holX,holY		= holPanel:GetPos()
	local walX,walY		= walPanel:GetPos()
	
	/* Get panels sizes */
	local isW,isH		= isPanel:GetSize()
	local runW,runH		= runPanel:GetSize()
	local holW,holH		= holPanel:GetSize()
	local walW,walH		= walPanel:GetSize()
	
	/* Resize */
	self:SetSize( sw5, 25 + isH + 5 + runH + 5 + holH + 5 + walH + 5 )
	
	/* Ensure proper size */
	local x,y			= self:GetPos()
	local w,h			= self:GetSize()
	if h > ScrH() - 10 then self:SetSize( w, ScrH() - 10 ) end
	
	/* Reposition */
	x,y			= self:GetPos()
	if ( y + h ) > ScrH() - 5 then self:SetPos( x, y - ( y + h - ScrH() - 5 ) ) end
end